﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace Maintain_supplier_information
{
    public partial class ChangeCollectionPointDetails : System.Web.UI.Page
    {
        StationeryInventoryEntities1 se;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                se = new StationeryInventoryEntities1();
                var q = from x in se.CollectionPoints
                        select x;
                ddl_CollectionPoint.DataSource = q.ToList();
                ddl_CollectionPoint.DataTextField = "CollectionPointName";
                ddl_CollectionPoint.DataValueField = "CollectionPointID";
                ddl_CollectionPoint.DataBind();

            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            se = new StationeryInventoryEntities1();

            int id = Convert.ToInt32(ddl_CollectionPoint.SelectedValue.ToString());

            CollectionPointDetail cp = new CollectionPointDetail();
            cp = se.CollectionPointDetails.FirstOrDefault(x => x.CollectionPointID == id && x.CollectionDate > DateTime.Today);
            if (cp != null)
            {
                txt_CollectionDate.Text = cp.CollectionDate.ToShortDateString();
                txt_CollectionPointTime.Text = cp.CollectionTime.ToShortTimeString();
            }
            else
            {
                txt_CollectionDate.Text = "";
                txt_CollectionPointTime.Text = "";
            }




        }

        protected void btn_Change_Click(object sender, EventArgs e)
        {

            se = new StationeryInventoryEntities1();
            int id = Convert.ToInt32(ddl_CollectionPoint.SelectedValue.ToString());
            if (Convert.ToDateTime(txt_CollectionDate.Text) > DateTime.Today)
            {
                CollectionPointDetail cp = new CollectionPointDetail();
                cp = se.CollectionPointDetails.FirstOrDefault(x => x.CollectionPointID == id && x.CollectionDate > DateTime.Today);
                if (cp != null)
                {
                    cp.CollectionDate = Convert.ToDateTime(txt_CollectionDate.Text);

                    cp.CollectionTime = Convert.ToDateTime(txt_CollectionPointTime.Text);

                    se.SaveChanges();
                    lbl_status.Text = "Updated";
                }
                else
                {
                    CollectionPointDetail cp1 = new CollectionPointDetail();
                    cp1.CollectionPointID = id;
                    cp1.CollectionDate = Convert.ToDateTime(txt_CollectionDate.Text);
                    cp1.CollectionTime = Convert.ToDateTime(txt_CollectionPointTime.Text);
                    se.CollectionPointDetails.Add(cp1);
                    se.SaveChanges();

                }
            }
            else
            {
                lbl_status.Text = "Date error";
            }


        }
    }
}